package com.knowif.pojo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.validation.constraints.NotEmpty;

import org.springframework.validation.annotation.Validated;

/**
 * @author xingyao
 * @Description:用户订单实体
 */
@Validated
public class UserOrder {
    private Integer id;

    /**
     * 自我介绍
     */
    private String introduction;

    /**
     * 约谈方式
     */
    private String meetType;

    /**
     * 订单金额
     */
    @NotEmpty(message="金额不能为空")
    private Double price;

    /**
     * 约谈问题
     */
    @NotEmpty(message="约谈问题不能为空")
    private String question;

    /**
     * 订单评分
     */
    private Integer star;

    /**
     * 老师id
     */
    @NotEmpty(message="导师id不能为空")
    private Integer toUserId;

    /**
     * 用户id
     */
    @NotEmpty(message="用户id不能为空")
    private Integer userId;

    /**
     * 订单状态
     */
    private Integer status;

    /**
     * 购买话题id
     * */
    private Integer topicId;
    
    private Topic topic;

    private TutorInfo tutorInfo;

    private TUser tUser;

    private List<UserComment> userComments;
    
    private List<UserOrderStatus> userOrderStatuses;

    private List<UserOrderFlow> userOrderFlows;

    public Integer getTopicId() {
        return topicId;
    }

    public void setTopicId(Integer topicId) {
        this.topicId = topicId;
    }

    public List<UserOrderStatus> getUserOrderStatuses() {
        return userOrderStatuses;
    }

    public void setUserOrderStatuses(List<UserOrderStatus> userOrderStatuses) {
        this.userOrderStatuses = userOrderStatuses;
    }

    public List<UserOrderFlow> getUserOrderFlows() {
        return userOrderFlows;
    }

    public void setUserOrderFlows(List<UserOrderFlow> userOrderFlows) {
        this.userOrderFlows = userOrderFlows;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getIntroduction() {
        return introduction;
    }

    public void setIntroduction(String introduction) {
        this.introduction = introduction == null ? null : introduction.trim();
    }

    public String getMeetType() {
        return meetType;
    }

    public void setMeetType(String meetType) {
        this.meetType = meetType == null ? null : meetType.trim();
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public String getQuestion() {
        return question;
    }

    public void setQuestion(String question) {
        this.question = question == null ? null : question.trim();
    }

    public Integer getStar() {
        return star;
    }

    public void setStar(Integer star) {
        this.star = star;
    }

    public Integer getToUserId() {
        return toUserId;
    }

    public void setToUserId(Integer toUserId) {
        this.toUserId = toUserId;
    }

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }
    
    
	public List<UserComment> getUserComments() {
		return userComments;
	}

	public void setUserComments(List<UserComment> userComments) {
		this.userComments = userComments;
	}

	public Topic getTopic() {
		return topic;
	}

	public void setTopic(Topic topic) {
		this.topic = topic;
	}

    public TutorInfo getTutorInfo() {
        return tutorInfo;
    }

    public void setTutorInfo(TutorInfo tutorInfo) {
        this.tutorInfo = tutorInfo;
    }

    public TUser gettUser() {
        return tUser;
    }

    public void settUser(TUser tUser) {
        this.tUser = tUser;
    }

    public static List<Integer> getUserOrderIds(List<UserOrder> userOrders){
    	if(userOrders == null || userOrders.isEmpty()){
    		return new ArrayList<Integer>();
    	}
    	List<Integer> userOrderIds = new ArrayList<Integer>();
    	for(UserOrder userOrder : userOrders){
    		userOrderIds.add(userOrder.getId());
    	}
    	return userOrderIds;
    }
    
    /**
     * @author: xingyao 
     * @Description:  往订单信息里面存评论信息
     * @param userOrders
     * @param userComments
     * @return
     */
    public static List<UserOrder> saveComments(List<UserOrder> userOrders,List<UserComment> userComments){
    	if(userOrders == null || userOrders.isEmpty()){
    		return new ArrayList<UserOrder>();
    	}
    	
    	if(userComments == null || userComments.isEmpty()){
    		return userOrders;
    	}
    	
    	//得到所有的开始评论
    	List<UserComment> userCommentsList = orderComment(userOrders, userComments);
    	//进行排序
    	Collections.sort(userCommentsList, UserComment.userCommentComparator);
    	//存放在Set里面
    	List<UserComment> userCommentsSet  = new ArrayList<UserComment>();
		for(UserComment userComment : userCommentsList){
    		userCommentsSet.add(userComment);
    		for(UserComment userComment2 : userComments){
    			if(userComment.getUserOrderId().equals(userComment2.getUserOrderId()) && !userComment.equals(userComment2)){
    				userCommentsSet.add(userComment2);
    			}
    		}
    	}
    	
		List<UserComment> result  = null;
    	for(UserOrder userOrder : userOrders){
    		result = new ArrayList<UserComment>();
    		for(UserComment userComment : userCommentsSet){
    			if(userOrder.getId().equals(userComment.getUserOrderId())){
    				result.add(userComment);
    			}
    		}
    		userOrder.setUserComments(result);
    	}
    	
		return userOrders;
    }
    
    
    /**
     * id进行排序
     */
    public static Comparator<UserOrder> userOrderComparator = new Comparator<UserOrder>() {
        public int compare(UserOrder u1, UserOrder u2) {
            return u1.getId() - u2.getId();
        }
    };
    

    public static List<UserComment> orderComment(List<UserOrder> userOrders,List<UserComment> userComments){
    	if(userOrders == null || userOrders.isEmpty()){
    		return new ArrayList<UserComment>();
    	}
    	List<UserComment> list = new ArrayList<UserComment>();
    	for(UserOrder userOrder : userOrders){
    		for(int i = 0;userComments != null && i<userComments.size();i++){
    			if((userOrder.getId()+"").equals(userComments.get(i).getUserOrderId()+"") && userComments.get(i).getParentId() == null){
    				list.add(userComments.get(i));
    			}
    		}
    	}
		return list;
    }

    public static List<UserOrder> addTUsers(List<TUser> tUsers,List<UserOrder> userOrders){
        if(userOrders == null || userOrders.isEmpty()){
            return new ArrayList<UserOrder>();
        }
        for(UserOrder userOrder : userOrders){
            for(int i = 0;tUsers != null && i<tUsers.size();i++){
                if(userOrder.userId.equals(tUsers.get(i).getId())){
                    userOrder.settUser(tUsers.get(i));
                }
            }
        }
        return userOrders;
    }

    public static List<Integer> getTUserIds(List<UserOrder> userOrders){
        if(userOrders == null || userOrders.isEmpty()){
            return new ArrayList<Integer>();
        }
        List<Integer> tUserId = new ArrayList<Integer>();
        for(UserOrder userOrder : userOrders){
            tUserId.add(userOrder.getUserId());
        }
        return tUserId;
    }

    public static List<UserOrder> addTutors(List<TutorInfo> tutorInfos,List<UserOrder> userOrders){
        if(userOrders == null || userOrders.isEmpty()){
            return new ArrayList<UserOrder>();
        }
        for(UserOrder userOrder : userOrders){
            for(int i = 0;tutorInfos != null && i<tutorInfos.size();i++){
               if(userOrder.getToUserId().equals(tutorInfos.get(i).getUserId())){
                   userOrder.setTutorInfo(tutorInfos.get(i));
               }
            }
        }
        return userOrders;
    }

    public static List<Integer> getTutorIds(List<UserOrder> userOrders){
        if(userOrders == null || userOrders.isEmpty()){
            return new ArrayList<Integer>();
        }
        List<Integer> tutorIds = new ArrayList<Integer>();
        for(UserOrder userOrder : userOrders){
            tutorIds.add(userOrder.getToUserId());
        }
        return tutorIds;
    }
    
    public static List<UserOrder> addTopics(List<Topic> topics,List<UserOrder> userOrders){
		if(userOrders == null || userOrders.isEmpty()){
			return new ArrayList<UserOrder>();
		}
		for(UserOrder userOrder : userOrders){
			for(int i = 0;topics != null && i<topics.size();i++){
			    if(userOrder.topicId.equals(topics.get(i).getId())){
                    userOrder.setTopic(topics.get(i));
                }
			}
		}
		return userOrders;
	}
    
    public static List<Integer> getTopicIds(List<UserOrder> userOrders){
    	if(userOrders == null || userOrders.isEmpty()){
    		return new ArrayList<Integer>();
    	}
    	List<Integer> topicIds = new ArrayList<Integer>();
    	for(UserOrder userOrder : userOrders){
    		topicIds.add(userOrder.getTopicId());
    	}
    	return topicIds;
    }
    
    
	public static void saveUserOrderFlows(List<UserOrder> userOrders,List<UserOrderFlow> userOrderFlows){
		if(userOrders == null || userOrders.isEmpty() || userOrderFlows == null ||userOrderFlows.isEmpty()){
			return;
		} 
		List<UserOrderFlow> userOrderFlowsResult = null;
		for(UserOrder userOrder : userOrders){ 
			userOrderFlowsResult = new ArrayList<UserOrderFlow>();
			for(UserOrderFlow userOrderFlow : userOrderFlows){
				if(userOrder.getId() != null && userOrder.getId().equals(userOrderFlow.getUserOrderId())){
					userOrderFlowsResult.add(userOrderFlow);
				}
			}
			userOrder.setUserOrderFlows(userOrderFlowsResult);
		}
	}
	
	
	
	public static void saveUserOrderStatuss(List<UserOrder> userOrders,List<UserOrderStatus> userOrderStatus){
		if(userOrders == null || userOrders.isEmpty() || userOrderStatus == null ||userOrderStatus.isEmpty()){
			return;
		} 
		List<UserOrderStatus> userOrderStatusResult = null;
		for(UserOrder userOrder : userOrders){ 
			userOrderStatusResult = new ArrayList<UserOrderStatus>();
			for(UserOrderStatus userOrderStatu : userOrderStatus){
				if(userOrder.getId() != null && userOrder.getId().equals(userOrderStatu.getUserOrderId())){
					userOrderStatusResult.add(userOrderStatu);
				}
			}
			userOrder.setUserOrderStatuses(userOrderStatusResult);
		}
	}


    @Override
    public String toString() {
        return "UserOrder{" +
                "id=" + id +
                ", introduction='" + introduction + '\'' +
                ", meetType='" + meetType + '\'' +
                ", price=" + price +
                ", question='" + question + '\'' +
                ", star=" + star +
                ", toUserId=" + toUserId +
                ", userId=" + userId +
                ", status=" + status +
                ", topicId=" + topicId +
                ", topic=" + topic +
                ", tutorInfo=" + tutorInfo +
                ", tUser=" + tUser +
                '}';
    }
}